/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.helpers.manto;

import com.uca.sisbec.commons.Utils;
import com.uca.sisbec.forms.manto.ReunionForm;
import com.uca.sisbec.mapping.Reunion;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

/**
 *
 * @author Alex
 */
public class ReunionHelper {
    private Session session;
    
    public ReunionHelper(Session session){
        this.session=session;
    }
    
    public boolean ValidateReunion(ReunionForm rf){
        if (ExisteReunionSimilarDB(rf).isEmpty())
            return false;
        return true;
    }
    
    public int InsertarReunionDB(ReunionForm rf){
        Reunion reu = new Reunion();
        reu.setTema(rf.getTema());
        reu.setLugar(rf.getLugar());
        reu.setHorafechaReunion(Utils.getUtilDatePicker(rf.getHorafechaReunion()));
        reu.setComentarios(rf.getComentarios());
        reu.setPonente(rf.getPonente());
        session.save(reu);
        return reu.getIdReunion();
    }
    
    public List<Reunion> BuscarReunionDB(ReunionForm rf){
        String hql = "from Reunion "
                + "where tema like :v_tema "
                + "and lugar like :v_lugar ";
        if (!Utils.isNullOrEmpty(rf.getHorafechaReunion()))
            hql += "and date_format(horafechaReunion, '%d/%m/%Y 00:00') = date_format(:v_fechaReu, '%d/%m/%Y 00:00')";  
        Query query = session.createQuery(hql);
        query.setString("v_tema", "%" + rf.getTema() + "%");
        query.setString("v_lugar", "%" + rf.getLugar() + "%");
        if (!Utils.isNullOrEmpty(rf.getHorafechaReunion()))
            query.setDate("v_fechaReu", Utils.getUtilDatePicker(rf.getHorafechaReunion()));
          List<Reunion> listReu = new ArrayList<Reunion>(query.list());
        return listReu;
    }
    
    public List<Reunion> ExisteReunionSimilarDB(ReunionForm rf){
        Criteria crit = session.createCriteria(Reunion.class);
        crit = crit.add(Expression.like("lugar",rf.getLugar()));
        crit = crit.add(Expression.eq("horafechaReunion", Utils.getUtilDatePicker(rf.getHorafechaReunion())));
        List<Reunion> listReu = new ArrayList<Reunion>(crit.list());
        return listReu;
    }
    
    
    /** Retorna objeto segun busqueda por codigo */
    public Reunion BuscarReunionDB(String cod){
        Reunion reu = (Reunion)session.get(Reunion.class,Integer.parseInt(cod));
        return reu;
    }
    
    
    /* Devuelve el objeto y lo pobla en el formulario */
    public void BuscarReunionDB(String cod, ReunionForm rf){
        Reunion reu = BuscarReunionDB(cod);
        rf.setTema(reu.getTema());
        rf.setIdReunion(reu.getIdReunion());
        rf.setLugar(reu.getLugar());
        rf.setHorafechaReunion(Utils.getUtilDatePicker(reu.getHorafechaReunion()));
        rf.setPonente(reu.getPonente());
        rf.setComentarios(reu.getComentarios());
    }
    
    public ReunionForm ModificarReunionDB(ReunionForm rf){
        Reunion reu = new Reunion();
        reu.setIdReunion(rf.getIdReunion());
        reu.setTema(rf.getTema());
        reu.setLugar(rf.getLugar());
        reu.setPonente(rf.getPonente());
        reu.setHorafechaReunion(Utils.getUtilDatePicker(rf.getHorafechaReunion()));
        reu.setComentarios(rf.getComentarios());
        session.saveOrUpdate(reu);
        return rf;
    }
    
    public void EliminarReunionDB(ReunionForm rf){
        Reunion reu = new Reunion();
        reu.setIdReunion(rf.getIdReunion());
        session.load(reu,reu.getIdReunion());
        session.delete(reu);
    }
}
